PyQT5 (四十二) 在QTableWidget表格中快速搜索定位到特定行 的案例

您所在的位置:网站首页 python tablewidget 输入 PyQT5 (四十二) 在QTableWidget表格中快速搜索定位到特定行 的案例

PyQT5 (四十二) 在QTableWidget表格中快速搜索定位到特定行 的案例

2024-07-14 17:54| 来源: 网络整理| 查看: 265

在表格中快速搜索定位到特定行 的案例 1.数据的定位:findItems 2.如果找到了满足条件的单元格,会定位到单元格所在的行:setSliderPositon(row) 3.搜索满足条件的Cell 以字符串开头的所有 4.搜索满足条件的Cell 完全匹配 5.搜索满足条件的Cell 包含内容

import sys from PyQt5 import QtPrintSupport, QtGui from PyQt5.QtCore import Qt, QMimeData, QDate, QDateTime, QTime, QStringListModel from PyQt5.QtGui import QIcon, QPainter, QBrush, QPixmap, QStandardItemModel, QStandardItem, QColor from PyQt5.QtPrintSupport import QPageSetupDialog, QPrinter, QPrintDialog from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QFormLayout, QLabel, QLineEdit, QPushButton, QGridLayout, \ QCalendarWidget, QVBoxLayout, QDateTimeEdit, QAction, QMainWindow, QTextEdit, QStatusBar, QFileDialog, QDialog, \ QTableView, QMessageBox, QListView, QListWidget, QHBoxLayout, QTableWidget, QTableWidgetItem, QAbstractItemView ''' 在表格中快速搜索定位到特定行 的案例 1.数据的定位:findItems 2.如果找到了满足条件的单元格,会定位到单元格所在的行:setSliderPositon(row) 3.搜索满足条件的Cell 以字符串开头的所有 4.搜索满足条件的Cell 完全匹配 5.搜索满足条件的Cell 包含内容 ''' class DataLocationDemo(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # 设置定位和左上角坐标 self.setGeometry(300, 300, 460, 360) # 设置窗口标题 self.setWindowTitle('QTableWidget扩展表格的单元格放置控件 的演示') # 设置窗口图标 # self.setWindowIcon(QIcon('../web.ico')) layout = QHBoxLayout() tablewidget = QTableWidget() tablewidget.setRowCount(40) tablewidget.setColumnCount(4) layout.addWidget(tablewidget) tablewidget.setHorizontalHeaderLabels(['姓名','性别','体重(kg)']) for i in range(40): for j in range(4): itemContent = '(%d行,%d列)' %(i,j) tablewidget.setItem(i,j,QTableWidgetItem(itemContent)) # 搜索满足条件的Cell #完全匹配 text = '(13行,1列)' items = tablewidget.findItems(text, Qt.MatchExactly) if len(items)>0: item = items[0] item.setBackground(QBrush(QColor(0,255,0))) item.setForeground(QBrush(QColor(255,0,0))) row = item.row() # 定位到指定行 tablewidget.verticalScrollBar().setSliderPosition(row) # 搜索满足条件的Cell 以字符串开头的所有 text = '(10行' items = tablewidget.findItems(text, Qt.MatchStartsWith) # items = tablewidget.findItems(text, Qt.MatchContains) # 包含内容 if len(items) > 0: if len(items) > 0: # 定位到第一行 tablewidget.verticalScrollBar().setSliderPosition(items[0].row()) for i in range(len(items)): items[i].setBackground(QBrush(QColor(0, 255, 0))) items[i].setForeground(QBrush(QColor(255, 0, 0))) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) # 设置应用图标 app.setWindowIcon(QIcon('../web.ico')) w = DataLocationDemo() w.show() sys.exit(app.exec_())



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3